<template>
  <!-- 砂处理信息填写表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="30%"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">产品名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.materialName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">调整时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.adjustsTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">紧实率</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.compactionRate }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">回数</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.numberCycles }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">混砂总量</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.mixSandTotalQuantity }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">日期</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.sandDate }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">备注</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.remarks }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">删除标识</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.deleted }}
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="160"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品" prop="materialCode" label-width="80px">
            <el-input
              v-model="formData.materialCode"
              placeholder="请输入产品"
              @click="showWorkOrderSelect"
            >
              <template #append>
                <el-button @click="resetMaterial"> 清除 </el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调整时间" prop="adjustsTime" label-width="80px">
            <el-date-picker
              v-model="formData.adjustsTime"
              type="datetime"
              placeholder="请选择调整时间"
              value-format="YYYY-MM-DD HH:mm:ss"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="紧实率" prop="compactionRate" label-width="80px">
            <el-input
              v-model="formData.compactionRate"
              placeholder="请输入紧实率"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="回数" prop="numberCycles" label-width="80px">
            <el-input
              v-model="formData.numberCycles"
              placeholder="请输入回数"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="混砂总量"
            prop="mixSandTotalQuantity"
            label-width="80px"
          >
            <el-input
              v-model="formData.mixSandTotalQuantity"
              placeholder="请输入混砂总量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日期" prop="sandDate" label-width="80px">
            <el-date-picker
              v-model="formData.sandDate"
              type="date"
              placeholder="请选择日期"
              value-format="YYYY-MM-DD"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks" label-width="80px">
            <el-input
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
    <conten-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :brekList="brekList"
    />
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESSANDDISPOSEREPORTADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  materialCode: [
    { required: true, message: "请选择产品", trigger: "change" },
  ],
  adjustsTime: [],
  compactionRate: [
    { pattern: /^\d{1,10}(\.\d{0,2})?$/, message: "请输入数字" },
  ],
  numberCycles: [{ pattern: /^\d{1,10}(\.\d{0,2})?$/, message: "请输入数字" }],
  mixSandTotalQuantity: [
    { pattern: /^\d{1,10}(\.\d{0,2})?$/, message: "请输入数字" },
  ],
  sandDate: [{ required: true, message: "请选择日期", trigger: "change"}],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  adjustsTime: "",
  compactionRate: "",
  numberCycles: "",
  mixSandTotalQuantity: "",
  sandDate: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        materialId,
        materialCode,
        materialName,
        adjustsTime,
        compactionRate,
        numberCycles,
        mixSandTotalQuantity,
        sandDate,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        materialId,
        materialCode,
        materialName,
        adjustsTime,
        compactionRate,
        numberCycles,
        mixSandTotalQuantity,
        sandDate,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.adjustsTime = "";
  formData.compactionRate = "";
  formData.numberCycles = "";
  formData.mixSandTotalQuantity = "";
  formData.sandDate = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 砂处理信息填写保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/messanddisposereport/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
};
//选择物料弹窗
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  if (data) {
    formData.materialId = data.id;
    formData.materialCode = data.materialCode;
    formData.materialName = data.materialName;
  }
};
// 回显的数据
const brekList = ref([]);
const resetMaterial = () => {
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
};
</script>
